#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

extern char **environ;

/**
 * Clear all environment variables and set new ones according to the command line arguments.
 * The arguments are in the format of "key=value".
 * If the key is already set, it will be overwritten.
 * If the key is not set, it will be added.
 * If the key is empty, it will be ignored.
 * If the value is empty, the key will be removed.
 * Special case: if the key is "BYE", the variable will be removed no matter what the value is.
 */
int main(int argc, char *argv[]) {
    clearenv();
    for (int i = 1; i < argc; i++) {
        char *eq = strchr(argv[i], '=');
        if (eq == NULL) {
            continue;
        }
        *eq = '\0';
        if (strcmp(argv[i], "BYE") == 0) {
            unsetenv(argv[i]);
        } else {
            setenv(argv[i], eq + 1, 1);
        }
    }
    
    char **ep = NULL;
    for (ep = environ; ep != NULL && *ep != NULL; ep++) {
        printf("%s\n", *ep);
    }
    return 0;
}